
import { IdManager } from "../../utils/util";
import { AddInteractiveShape, CanvasContext } from "./designType";
import { Frame } from "./Frame";
import { Point } from "./Point";
import { ToolType } from "./ToolManager";


// Export the TreeNode class


export class Window {
  public id: string;
  public scale: number = 1;
  public originPoint: Point = new Point(0, 0);
  public frame?: Frame;       // 窗口形状
  private addInteractiveShape: AddInteractiveShape;
  public connections: { [direction: string]: { window: Window; angle: number } } = {};

  constructor(addInteractiveShape: AddInteractiveShape) { 
    this.id = IdManager.getNextId("window");
    this.addInteractiveShape = addInteractiveShape;
  }
  setOriginPoint(origin: Point) {
    this.originPoint = origin;
  }
  // 连接当前窗口到另一个窗口，指定方向和角度
  connect(direction: "left" | "right" | "top" | "bottom", targetWindow: Window, angle: number) {
    this.connections[direction] = { window: targetWindow, angle };
    switch (direction) {
      case "left":
        targetWindow.connections["right"] = { window: this, angle };
        break;
      case "right":
        targetWindow.connections["left"] = { window: this, angle };
        break;
      case "top":
        targetWindow.connections["bottom"] = { window: this, angle };
        break;
      case "bottom":
        targetWindow.connections["top"] = { window: this, angle };
        break;
    }
  }

  addMullion(point: Point, mullionType: ToolType) {
    if (!this.frame) {
      wx.showToast({
        title: "请先绘制窗框",
        icon: "none",               // 设置为无图标样式
        duration: 2000               // 显示时间（单位为毫秒）
      });
    }
    switch (mullionType) {
      case "horizontalMullion":
        break;
      case "verticalMullion":
        break;
      default:
      wx.showToast({
        title: "请选择梃",
        icon: "none", // 设置为无图标样式
        duration: 2000 // 显示时间（单位为毫秒）
      });
      break;
      
    }

  }
  addFrame(point: Point) {
    if (this.frame) {
      wx.showToast({
        title: "请重新绘画 或者先清除当前窗户",
        icon: "none",               // 设置为无图标样式
        duration: 2000               // 显示时间（单位为毫秒）
      });
      return;
    }
    if (point.type !== "rect") {
      wx.showToast({
        title: "请选择矩形框工具。",
        icon: "none",               // 设置为无图标样式
        duration: 2000               // 显示时间（单位为毫秒）
      });
      return;
    }

    // 使用传入的四个顶点创建形状
    this.frame = new Frame(this.addInteractiveShape.bind(this));
    this.frame.setShape(point);
    // 设置 dimension
  }


  // 绘制窗口，包括形状、中梃和窗扇
  setScale(scale: number): void {
    this.scale = scale;
  }
  draw(ctx: CanvasContext): void {
    if (this.frame) this.frame.draw(ctx);
    this.drawDimension(ctx);
    
  }
  private drawDimension(ctx: CanvasContext): void {
    if (this.frame) this.frame.drawDimension(ctx);
    
    // if (this.mullion) this.mullion.draw(ctx);
    // if (this.opening) this.opening.draw(ctx);
  }

  private drawTopView(ctx: CanvasContext ): void {
    if(this.frame) this.frame.drawTopView(ctx);
  }
}